import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/good_item_arr.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/search_bar.dart';
import 'package:xinxuan/common/sort_item.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/controllers/home_controller/search_record_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/utils/refresh_util.dart';
import 'package:get/get.dart';

class SearchRecordPage extends StatelessWidget {

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    String title = argumentsMap['title'] ?? '商品列表';
    return Scaffold(
      backgroundColor: pageBgColor,
      resizeToAvoidBottomInset: false,
      appBar: AppBarUtil(
        title: title,
        backgroundColor: pageBgColor,
      ),
      body: GetBuilder<SearchRecordPageController>(
        init: SearchRecordPageController(),
        builder: (controller) {
          return Column(
            children: [
              Container(
                height: 99.h,
                color: bgWhiteColor,
                child: Column(
                  children: [
                    SearchBar(
                      commonPageController: commonPageController, 
                      defaultValue: controller.keyword,
                      onChanged: (String value) {
                        controller.keyword = value;
                      }, 
                      onSubmitted: (String value) {
                        controller.keyword = value;
                        controller.getProductsList();
                      }
                    ),
                    Obx(() => Container(
                      height: 45.h,
                      child: Row(
                        children: [
                          SortItem(
                            title: '综合', 
                            index: 0, 
                            showBorder: 0 == controller.sortIndex.value, 
                            onTap: () {
                              controller.sortIndex.value = 0;
                              controller.getProductsList();
                            }
                          ),
                          SortItem(
                            title: '销量', 
                            index: 1, 
                            showBorder: 1 == controller.sortIndex.value, 
                            onTap: () {
                              controller.sortIndex.value = 1;
                              controller.getProductsList();
                            }
                          ),
                          controller.sortIndex.value == 2 ? SortItem(
                            title: '价格', 
                            index: 2, 
                            showBorder: 2 == controller.sortIndex.value, 
                            isPriceSort: true,
                            onTap: () {
                              controller.sortIndex.value = 3;
                              controller.priceSortDesc.value = true;
                              controller.getProductsList();
                            }
                          ) : SortItem(
                            title: '价格', 
                            index: 3, 
                            showBorder: 3 == controller.sortIndex.value, 
                            isPriceSort: true,
                            onTap: () {
                              controller.sortIndex.value = 2;
                              controller.priceSortDesc.value = false;
                              controller.getProductsList();
                            }
                          )
                        ],
                      ),
                    ))
                  ],
                ),
              ),
              Expanded(child: controller.productsListVo.value.data!.length <= 0 ? Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      'assets/icons/goods_list_def.png',
                      width: 273.w,
                      height: 146.h,
                    ),
                    Text(
                      '没有数据哦~',
                      style: TextStyle(
                        color: textSubtitleColor,
                        fontSize: 14.sp
                      ),
                    )
                  ],
                ),
              ) : ScrollConfigurationCommon(child: RefreshUtil.easyRefresh(
                onLoad: () async {
                  controller.onloadProductsList();
                },
                onRefresh: () async {
                  controller.getProductsList();
                },
                slivers: [
                  // SliverToBoxAdapter(
                  //   child: Container(
                  //     child: GoodItemArr(goodArr: controller.productsListVo.value.data!),
                  //   ),
                  // ),
                  GoodItemArr(goodArr: controller.productsListVo.value.data!)
                ]
              )))
            ],
          );
        },
      ),
    );
  }


}